<?php
/*
PWision extension: Newspaper http://pw-newspaper.googlecode.com/
Copyright (C) 2010--12 Becheru Petru-Ioan

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
include_once('inc/PBBCodeInput.inc');
include_once("inc/PFormSection.inc");
include_once("inc/PTable.inc");
include_once('inc/PRedirectSection.inc');
include_once("pw-newspaper.inc");


/** 	The EditArticle class is designed to model a section that allows a user to edit articles. It provides an intuitive interface with textboxes.
	*	\latexonly  \label{PWision:Extensions:Newspaper:EditArticle} \index{extension} \index{newspaper} \index{article} \endlatexonly
	*	\brief an article editor section.
	*	\version 1.8.3
	*	\todo check if someone else is editing this article
	*/
class EditArticle extends PFormSection
	{
	function Head()
		{
		return parent::Head()
			->Insert( PBBCodeInput::Head(Main()->getLanguage(),array('textContent', 'title', 'authors', 'keywords', 'logs', 'absTitle', 'textAbstract')) )
			;
		}
	protected function preprocessParam($param_name)
		{
		$_REQUEST[$param_name]=charset_decode_utf_8($_REQUEST[$param_name]);
		$_REQUEST[$param_name]=str_replace('&#160;',' ',$_REQUEST[$param_name]);// nbsp as a special char not allowed. fix for a chromium bug
		}
	function setParams()
		{
		$fulltext='/^[ -~\xA\xD\t]*$/i';//match printable ASCII + LF and CR
		$URL='`(^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?$)|(^$)`i';
		$this
			->addParam('ID','/^[0-9]{1,10}$/')
			->addParam('title',$fulltext,'')
			->addParam('absTitle',$fulltext,'')
			->addParam('textAbstract',$fulltext,'')
			->addParam('text',$fulltext,'')
			->addParam('authors','/^[-*&#;,.a-z0-9 ]{0,1024}$/i','')
			->addParam('affiliations',$fulltext,'')
			->addParam('Volume','/^[0-9]{0,10}$/i','')
			->addParam('Issue','/^[-+&#;,.a-z0-9 ()]{0,16}$/i','')
			->addParam('firstPage','/^[0-9]{0,10}$/i','')
			->addParam('lastPage','/^[0-9]{0,10}$/i','')
			->addParam("pdfURL",$URL,'')
			->addParam("sourceURL",$URL,'')
			->addParam("coverURL",$URL,'')
			->addParam('Language','/^[-_a-z]{0,5}$/i','')
			->addParam('absLanguage','/^[-_a-z]{0,5}$/i','')
			->addParam('KwLanguage','/^[-_a-z]{0,5}$/i','')
			->addParam('keywords','/^[-+&#;:\/,a-z0-9. ()]{0,1024}$/i','')
			->addParam('classifications','/^[-&#;:\/,a-z0-9. ]{0,1024}$/i','')
			->addParam('logs',$fulltext,'')
			->addParam('sub_articles','/^[,0-9 ]{0,2024}$/i','')
			->addParam('collection','/^[,0-9 ]{0,256}$/i','')
			;
		}
	function ProcessOK()
		{
		$rep=array(
			"'"=>'&#39;',// apostrophe used by SQL insert
			'<'=>'&lt;' // < special char of HTML
			);
		$article=new ArticleInfo;
			$article->ID			=$this->getParam('ID');
			$article->Title		=str_replace(array_keys($rep),array_values($rep),$this->getParam('title'));
			$article->absTitle=str_replace(array_keys($rep),array_values($rep),$this->getParam('absTitle'));
			$article->textAbstract		=str_replace(array_keys($rep),array_values($rep),$this->getParam('textAbstract'));
			$article->text			=str_replace(array_keys($rep),array_values($rep),$this->getParam('text'));
			$article->authors		=explodeNtrim( $this->getParam('authors') );
			$article->affiliations=explodeNtrim( str_replace(array_keys($rep),array_values($rep),$this->getParam('affiliations')) ,"\n" );
			$article->Volume		=$this->getParam('Volume');
			$article->Issue		=$this->getParam('Issue');
			$article->firstPage	=$this->getParam('firstPage');
			$article->lastPage	=$this->getParam('lastPage');
			$article->pdfURL		=$this->getParam('pdfURL');
			$article->sourceURL	=$this->getParam('sourceURL');
			$article->coverURL	=$this->getParam('coverURL');
			$article->Language	=$this->getParam('Language');
			$article->absLanguage	=$this->getParam('absLanguage');
			$article->KwLanguage	=$this->getParam('KwLanguage');
			$article->keywords	=explodeNtrim( $this->getParam('keywords') );
			$article->classifications	=explodeNtrim( $this->getParam('classifications') );
			$article->logs			=explodeNtrim( str_replace(array_keys($rep),array_values($rep),$this->getParam('logs')) ,"\n" );
			$article->sub_articles=explodeNtrim( $this->getParam('sub_articles') );
			$article->owners		=explodeNtrim( $this->getParam('collection') );
		if(
			( $article->ID = NewsPaper::EditArticle($article) )
			)
				{
				$this->AddMessage(
					'Article saved as '.
						Sections::Link2(
							'Article',
							$article->Title,
							$article->Title,
							Object(new PURLAttributes)
								->setAtr('ID',$article->ID)
							)->Draw()
					);
				//return true;
				}
			else
				$this->AddMessage("Article NOT saved!");
		return false;
		}
	/**
		*	\brief display a TValue list.
		*	\version 1.3.0
		*	\param $arr list
		*	\param $separator separator string
		*	\return a PContent with the tags separated by separator string
		*/
	static function ArticleTagListDraw($arr=array(), $separator=', ',$rels=null,$el_rel_sep='*',$rel_sep=' ')
		{
		$ret=new PContent;
		if(count($arr))
				{
				foreach($arr as $key=>$TValue)
					{
					$ret
						->Insert(
							new PText(
								$TValue
								)
							)
						 ;
					if($rels && array_key_exists($key+1,$rels))
						{
						$ret
							->Insert(new PText($el_rel_sep) )
							;
// 						print("[".$key.']rel=');
// 						print_r($rels);
						foreach(array_reverse($rels[$key+1]) as $rel)
							{
							$ret
								->Insert(new PText($rel))
								->Insert(new PText($rel_sep) )
								;
							}
						$ret->Pop();
						}
					$ret
						->Insert(new PText($separator) )
						;
					}
				$ret->Pop();//remove the last separator
				}
		return $ret;
		}
	function createForm()
		{
		$FormContent=new PContent();
		if	($this->wasParamSet('ID') && $this->didParamCheckedRegExp('ID') && // param ID is set and checks its reg_exp ; see PSection::CheckParams()
			($article=NewsPaper::getArticleInfo($this->getParam('ID')) )!=null  )
				{
				$T=Object(new PTable())
					->setAtr('style','display:block;width:100%;')
					;
						$T->AddCell()->Insert(
							Sections::Link2(
								'EditArticle',
								"edit this article",
								$this->getParam('ID'),
								Object(new PURLAttributes)
									->setAtr('ID',$this->getParam('ID'))
								)
							);
						$T->AddCell()->Insert(PTextlang("ID"))
							->Insert(new PInput("hidden",$this->getParam('ID'),"ID"));
					$T->AddRow();
						$T->AddCell()->Insert( PTextlang("Title") );
						$T->AddCell()->Insert(
							TextArea("title",$this->Params['title'][3]?$this->getParam('title'):$article->Title , 4)
							);
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("Language") ) );
						$T->AddCell()->Insert(new PInput("text",
							$this->Params['Language'][3]?$this->getParam('Language'): $article->Language,
							"Language"));
					$T->AddRow();
						$T->AddCell()->Insert( PTextlang("Abstract title") );
						$T->AddCell()->Insert(
							TextArea("absTitle",$this->Params['absTitle'][3]?$this->getParam('absTitle'):$article->absTitle , 4)
							);
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("Abstract language") ) );
						$T->AddCell()->Insert(new PInput("text",
							$this->Params['absLanguage'][3]?$this->getParam('absLanguage'): $article->absLanguage,
							"absLanguage"));
					$T->AddRow();
						$T->AddCell()->Insert( PTextlang("Abstract") );
						$T->AddCell()->Insert(
							TextArea("textAbstract",$this->Params['textAbstract'][3]?$this->getParam('textAbstract'):$article->textAbstract , 10)
							);
					$T->AddRow();
						$T->AddCell()->Insert(new PText( lang("affiliations").'<br/>'.lang("must be separated by newlines") ) );
						$T->AddCell()->Insert(
							TextArea(
								'affiliations',
								($this->Params['affiliations'][3]?$this->getParam('affiliations'): EditArticle::ArticleTagListDraw($article->affiliations,"\n") ),
								3
								)
								->setAtr('wrap','off')
							);
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("authors").'<br/>'.lang("must be separated by comma") ) );
						$T->AddCell()->Insert(
							TextArea('authors',$this->Params['authors'][3]?$this->getParam('authors'): EditArticle::ArticleTagListDraw($article->authors,', ',$article->authorAffiliations),3)
							);
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("volume") ) );
						$T->AddCell()->Insert(new PInput("number",
							$this->Params['Volume'][3]?$this->getParam('Volume'): $article->Volume,
							"Volume"));
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("issue") ) );
						$T->AddCell()->Insert(new PInput("text",
							$this->Params['Issue'][3]?$this->getParam('Issue'): $article->Issue,
							"Issue"));
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("pages") ) );
						$T->AddCell()
							->Insert(new PInput("number",
								$this->Params['firstPage'][3]?
									$this->getParam('firstPage'): 
									$article->firstPage,
								"firstPage"))
							->Insert(new PText('&ndash;'))
							->Insert(new PInput("number",
								$this->Params['lastPage'][3]?
									$this->getParam('lastPage'): 
									$article->lastPage,
								"lastPage"))
							;
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("pdfURL") ) );
						$T->AddCell()->Insert(new PInput("url",
							$this->Params['pdfURL'][3]?$this->getParam('pdfURL'): $article->pdfURL,
							"pdfURL"));
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("sourceURL") ) );
						$T->AddCell()->Insert(new PInput("url",
							$this->Params['sourceURL'][3]?$this->getParam('sourceURL'): $article->sourceURL,
							"sourceURL"));
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("coverURL") ) );
						$T->AddCell()->Insert(new PInput("url",
							$this->Params['coverURL'][3]?$this->getParam('coverURL'): $article->coverURL,
							"coverURL"));
					$T->AddRow();
						$T->AddCell()->Insert(new PText( lang("keywords").'<br/>'.lang("must be separated by comma") ) );
						$T->AddCell()->Insert(
							TextArea('keywords',$this->Params['keywords'][3]?$this->getParam('keywords'): EditArticle::ArticleTagListDraw($article->keywords) ,3)
							);
					$T->AddRow();
						$T->AddCell()->Insert( new PText( lang("KwLanguage") ) );
						$T->AddCell()->Insert(new PInput("text",
							$this->Params['KwLanguage'][3]?$this->getParam('KwLanguage'): $article->KwLanguage,
							"KwLanguage"));
					$T->AddRow();
						$T->AddCell()->Insert(new PText( lang("classifications").'<br/>'.lang("must be separated by comma") ) );
						$T->AddCell()->Insert(
							TextArea('classifications',$this->Params['classifications'][3]?$this->getParam('classifications'): EditArticle::ArticleTagListDraw($article->classifications) ,3)
							);
					$T->AddRow();
						$T->AddCell()->Insert(new PText( lang("logs").'<br/>'.lang("must be separated by newlines") ) );
						$T->AddCell()->Insert(
							TextArea(
								'logs',
								'-'.gmdate('Y-m-d H:i:s')."\n".
								($this->Params['logs'][3]?$this->getParam('logs'): EditArticle::ArticleTagListDraw($article->logs,"\n") ),
								3
								)
								->setAtr('wrap','off')
							);
					$T->AddRow();
						$T->AddCell()->Insert(new PText( lang("subarticles").'<br/>'.lang("must be separated by comma") ) );
						$T->AddCell()->Insert(new PInput("text",
							$this->Params['sub_articles'][3]?$this->getParam('sub_articles'): EditArticle::ArticleTagListDraw(array_column($article->sub_articles,'ID')) ,
							"sub_articles"));
					$T->AddRow();
						$T->AddCell()->Insert(new PText( lang("part of").'<br/>'.lang("must be separated by comma") ) );
						$T->AddCell()->Insert(new PInput("text",
							$this->Params['collection'][3]?$this->getParam('collection'): EditArticle::ArticleTagListDraw(array_column($article->owners,'ID')) ,
							"collection"));
					$FormContent
						->Insert($T)
						->Insert(
							new PBBCodeInput(
								$this->Params['text'][3]?$this->getParam('text'):$article->text,
								'text',
								'textContent'
								)
							)
						->Insert(
							Sections::Link2(
								'rmArticle',
								'',
								lang('remove this article!'),
								Object(new PURLAttributes)
									->setAtr('ID',$this->getParam('ID') )
								)
							)
						;
				}
			else
				{
				$FormContent
					->Insert(PTextlang('Enter the ID of the article:') )
					->Insert( new PInput("text",'','ID') )
					->Insert(new PInput("submit",lang("Edit!"),""))
					;
				}

		return PFormSection::createForm()
				->setAtr("method",'post')

			->Insert($FormContent)
			;
		}
	}

?>